package cn.peng.stu4teach.pages.home;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.LogUtils;
import com.king.camera.scan.CameraScan;

import cn.peng.stu4teach.R;
import cn.peng.stu4teach.databinding.FmHomeBinding;
import cn.peng.stu4teach.pages.home.adapter.CommFuncsAdapter;
import cn.peng.stu4teach.pages.qrscan.QrScanActivity;
import cn.peng.stu4teach.util.base.BaseFragment;

/**
 * 首页
 * A simple {@link BaseFragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to create an instance of this fragment.
 */
public class HomeFragment extends BaseFragment<FmHomeBinding, HomeFmVm> {
    private ActivityResultLauncher<Intent> mLauncher;

    private HomeFragment () {
    }

    public static HomeFragment newInstance () {
        return new HomeFragment();
    }

    @Override
    public int seLayoutId () {
        return R.layout.fm_home;
    }

    @Override
    public int setBrVmId () {
        return BR.vm;
    }

    @Override
    public HomeFmVm setBrVm () {
        return new ViewModelProvider(this).get(HomeFmVm.class);
    }

    @Override
    public void initData () {
        super.initData();

        mLauncher = getActivityLauncher();
        mVm.initBanner(mBinding.bannerHomeFm);
        mVm.initCommFuncsRv(mBinding.rvCommFunc);
        mVm.initMsgTypeRv(mBinding.rvMsgType);

        mVm.registerBroadCaseReceiver((CommFuncsAdapter) mBinding.rvCommFunc.getAdapter());
    }

    @Override
    public void initViewObservable () {
        super.initViewObservable();

        // 权限请求的监听
        mVm.permissionRequesting.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged (Boolean pBoolean) {
                if (pBoolean) {
                    mVm.requestPermission(mFragment);
                }
            }
        });

        // 开启扫码的监听
        mVm.startQrScan.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged (Boolean pBoolean) {
                if (!pBoolean || null == mLauncher) {
                    return;
                }

                Intent intent = new Intent(mContext, QrScanActivity.class);
                mLauncher.launch(intent);
            }
        });
    }

    /**
     * CnPeng:2024/6/16 19:00 获取 ActivityLauncher 以启动新 Activity 并获取结果
     */
    private ActivityResultLauncher<Intent> getActivityLauncher () {
        return registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult (ActivityResult pRe) {
                        if (null == pRe || null == pRe.getData() || pRe.getResultCode() != RESULT_OK) {
                            return;
                        }

                        String scanRe = pRe.getData().getStringExtra(CameraScan.SCAN_RESULT);
                        LogUtils.d(scanRe);
                    }
                });
    }

    @Override
    public void onDestroy () {
        super.onDestroy();
        mVm.unregisterBroadCaseReceiver();
    }
}